﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Web.SessionState;
using Model;
using DAL;
using System.IO;
using System.Dynamic;

namespace PinDanUI.admin.tools
{
    /// <summary>
    /// Admin 的摘要说明
    /// </summary>
    public class Admin : IHttpHandler, IRequiresSessionState
    {
        static Dictionary<string, Func<string>> hs = new Dictionary<string, Func<string>>();
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string act = JobRequest.GetRequestString("act");
            string result = string.Empty;
            if (!string.IsNullOrEmpty(act) && hs.ContainsKey(act))
            {
                result = hs[act]();
            }
            context.Response.Write(result);
            context.Response.End();
        }
        static Admin()
        {
            #region 登录后台
            hs.Add("Login", () =>
            {
                string username = JobRequest.GetRequestString("username");
                string userpwd = DESEncrypt.Encrypt(JobRequest.GetRequestString("userpwd"));
                AdminInfo info = AdminDAL.Login(username,userpwd);
                if(info.username!="")
                {
                    B.MyAdmin = info;
                    return Commons.GetSuccess("操作成功", "");
                }
                else
                {
                    return Commons.GetFaild("操作失败");
                }
            });
            #endregion
            #region 保存产品
            hs.Add("Save_Product", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                int typeid = JobRequest.GetRequestInt("typeid"); //分类id
                string name = JobRequest.GetRequestString("name"); //标题
                string img = JobRequest.GetRequestString("img");
                string vname = JobRequest.GetRequestString("vname"); //视频名称
                string path = JobRequest.GetRequestString("path");
                string vpath = JobRequest.GetRequestString("vpath");
                int positions = JobRequest.GetRequestInt("positions");
                string old_img = JobRequest.GetRequestString("old_img");
                string old_vpath = JobRequest.GetRequestString("old_vpath");
                int click = JobRequest.GetRequestInt("click");
                int sort = JobRequest.GetRequestInt("sort");
                string context = HttpContext.Current.Server.UrlDecode(JobRequest.GetRequestString("context")); //内容
                bool imgchange = JobRequest.GetRequestBool("imgchange");//图片是否有改变
                bool vpathchange = JobRequest.GetRequestBool("vpathchange");//图片是否有改变
                bool istop = JobRequest.GetRequestBool("istop");//是否推荐
                //参数

                string new_img = "";
                if (imgchange && !string.IsNullOrEmpty(img))
                {
                    try
                    {
                        //删除旧图片  之前这段代码在下边，经常出异常，放上边就可以了
                        if (!string.IsNullOrEmpty(old_img))
                        {
                            List<string> imgs = new List<string>() { old_img };
                            Commons.DeleteImg(imgs);
                        }

                        string old_path = img.Substring(0, img.LastIndexOf('/'));
                        new_img = img.Replace("temp/", "");
                        string new_path = new_img.Substring(0, new_img.LastIndexOf('/') + 1);
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(new_path)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(new_path));
                            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(old_path));
                            foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
                            {
                                fi.MoveTo(HttpContext.Current.Server.MapPath(new_path) + fi.Name);
                            }
                            Directory.Delete(HttpContext.Current.Server.MapPath("/upload/temp/" + "/"), true);
                        }


                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    new_img = img;
                }

                string new_vpath = "";
                if (vpathchange && !string.IsNullOrEmpty(vpath))
                {
                    try
                    {
                        //删除旧图片  之前这段代码在下边，经常出异常，放上边就可以了
                        if (!string.IsNullOrEmpty(old_vpath))
                        {
                            List<string> imgs = new List<string>() { old_vpath };
                            Commons.DeleteImg(imgs);
                        }

                        string old_path = vpath.Substring(0, vpath.LastIndexOf('/'));
                        new_vpath = vpath.Replace("temp/", "");
                        string new_path = new_vpath.Substring(0, new_vpath.LastIndexOf('/') + 1);
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(new_path)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(new_path));
                            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(old_path));
                            foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
                            {
                                fi.MoveTo(HttpContext.Current.Server.MapPath(new_path) + fi.Name);
                            }
                            Directory.Delete(HttpContext.Current.Server.MapPath("/upload/temp/" + "/"), true);
                        }


                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    new_vpath = vpath;
                }
                ProductInfo info = new ProductInfo()
                {
                    id=id,
                    typeid = typeid,
                    name = name,
                    img = new_img,
                    vname=vname,
                    path = path,
                    click = click,
                    sort=sort,
                    context=context,
                    istop=istop,
                    positions=positions,
                    vpath=new_vpath,
                    createdate=DateTime.Now
                };
                bool ok = false;
                if (id > 0)
                {
                    ok = ProductDAL.Update(info);
                }
                else
                {
                    ok = ProductDAL.Add(info);
                }
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 保存画册
            hs.Add("Save_Img", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                int typeid = JobRequest.GetRequestInt("typeid"); //分类id
                string name = JobRequest.GetRequestString("name"); //标题
                string img = JobRequest.GetRequestString("img");
                string vpath = JobRequest.GetRequestString("vpath");
                int positions = JobRequest.GetRequestInt("positions");
                string old_img = JobRequest.GetRequestString("old_img");
                string old_vpath = JobRequest.GetRequestString("old_vpath");
                int click = JobRequest.GetRequestInt("click");
                string context = HttpContext.Current.Server.UrlDecode(JobRequest.GetRequestString("context")); //内容
                bool imgchange = JobRequest.GetRequestBool("imgchange");//图片是否有改变
                bool vpathchange = JobRequest.GetRequestBool("vpathchange");//图片是否有改变
                bool istop = JobRequest.GetRequestBool("istop");//是否推荐
                //参数

                string new_img = "";
                if (imgchange && !string.IsNullOrEmpty(img))
                {
                    try
                    {
                        //删除旧图片  之前这段代码在下边，经常出异常，放上边就可以了
                        if (!string.IsNullOrEmpty(old_img))
                        {
                            List<string> imgs = new List<string>() { old_img };
                            Commons.DeleteImg(imgs);
                        }

                        string old_path = img.Substring(0, img.LastIndexOf('/'));
                        new_img = img.Replace("temp/", "");
                        string new_path = new_img.Substring(0, new_img.LastIndexOf('/') + 1);
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(new_path)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(new_path));
                            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(old_path));
                            foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
                            {
                                fi.MoveTo(HttpContext.Current.Server.MapPath(new_path) + fi.Name);
                            }
                            Directory.Delete(HttpContext.Current.Server.MapPath("/upload/temp/" + "/"), true);
                        }


                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    new_img = img;
                }
                string new_vpath = "";
                if (vpathchange && !string.IsNullOrEmpty(vpath))
                {
                    try
                    {
                        //删除旧图片  之前这段代码在下边，经常出异常，放上边就可以了
                        if (!string.IsNullOrEmpty(old_vpath))
                        {
                            List<string> imgs = new List<string>() { old_vpath };
                            Commons.DeleteImg(imgs);
                        }

                        string old_path = vpath.Substring(0, vpath.LastIndexOf('/'));
                        new_vpath = vpath.Replace("temp/", "");
                        string new_path = new_vpath.Substring(0, new_vpath.LastIndexOf('/') + 1);
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(new_path)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(new_path));
                            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(old_path));
                            foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
                            {
                                fi.MoveTo(HttpContext.Current.Server.MapPath(new_path) + fi.Name);
                            }
                            Directory.Delete(HttpContext.Current.Server.MapPath("/upload/temp/" + "/"), true);
                        }


                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    new_vpath = vpath;
                }
                ImgInfo info = new ImgInfo()
                {
                    id = id,
                    typeid = typeid,
                    name = name,
                    img = new_img,
                    click = click,
                    context = context,
                    istop = istop,
                    positions = positions,
                    vpath = new_vpath,
                    createdate = DateTime.Now
                };
                bool ok = false;
                if (id > 0)
                {
                    ok = ImgDAL.Update(info);
                }
                else
                {
                    ok = ImgDAL.Add(info);
                }
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 保存资讯
            hs.Add("Save_News", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                string name = JobRequest.GetRequestString("name"); //标题
                string img = JobRequest.GetRequestString("img");
                string old_img = JobRequest.GetRequestString("old_img");
                int click = JobRequest.GetRequestInt("click");
                string context = HttpContext.Current.Server.UrlDecode(JobRequest.GetRequestString("context")); //内容
                bool imgchange = JobRequest.GetRequestBool("imgchange");//图片是否有改变
                //参数
                string new_img = "";
                if (imgchange && !string.IsNullOrEmpty(img))
                {
                    try
                    {
                        //删除旧图片  之前这段代码在下边，经常出异常，放上边就可以了
                        if (!string.IsNullOrEmpty(old_img))
                        {
                            List<string> imgs = new List<string>() { old_img };
                            Commons.DeleteImg(imgs);
                        }

                        string old_path = img.Substring(0, img.LastIndexOf('/'));
                        new_img = img.Replace("temp/", "");
                        string new_path = new_img.Substring(0, new_img.LastIndexOf('/') + 1);
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(new_path)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(new_path));
                            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(old_path));
                            foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
                            {
                                fi.MoveTo(HttpContext.Current.Server.MapPath(new_path) + fi.Name);
                            }
                            Directory.Delete(HttpContext.Current.Server.MapPath("/upload/temp/" + "/"), true);
                        }


                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    new_img = img;
                }
                NewsInfo info = new NewsInfo()
                {
                    id = id,
                    name = name,
                    img=new_img,
                    click = click,
                    context = context,
                    createdate = DateTime.Now
                };
                bool ok = false;
                if (id > 0)
                {
                    ok = NewsDAL.Update(info);
                }
                else
                {
                    ok = NewsDAL.Add(info);
                }
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 保存幻灯片
            hs.Add("Save_Ad", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                string name = JobRequest.GetRequestString("name"); //标题
                string img = JobRequest.GetRequestString("img");
                string link = JobRequest.GetRequestString("link");
                string old_img = JobRequest.GetRequestString("old_img");
                int click = JobRequest.GetRequestInt("click");
                string context = HttpContext.Current.Server.UrlDecode(JobRequest.GetRequestString("context")); //内容
                bool imgchange = JobRequest.GetRequestBool("imgchange");//图片是否有改变
                //参数

                string new_img = "";
                if (imgchange && !string.IsNullOrEmpty(img))
                {
                    try
                    {
                        //删除旧图片  之前这段代码在下边，经常出异常，放上边就可以了
                        if (!string.IsNullOrEmpty(old_img))
                        {
                            List<string> imgs = new List<string>() { old_img };
                            Commons.DeleteImg(imgs);
                        }

                        string old_path = img.Substring(0, img.LastIndexOf('/'));
                        new_img = img.Replace("temp/", "");
                        string new_path = new_img.Substring(0, new_img.LastIndexOf('/') + 1);
                        if (!System.IO.Directory.Exists(HttpContext.Current.Server.MapPath(new_path)))
                        {
                            Directory.CreateDirectory(HttpContext.Current.Server.MapPath(new_path));
                            DirectoryInfo di = new DirectoryInfo(HttpContext.Current.Server.MapPath(old_path));
                            foreach (FileInfo fi in di.GetFiles())//可以用 di.GetFiles("*.jpg") 只移动jpg文件
                            {
                                fi.MoveTo(HttpContext.Current.Server.MapPath(new_path) + fi.Name);
                            }
                            Directory.Delete(HttpContext.Current.Server.MapPath("/upload/temp/" + "/"), true);
                        }


                    }
                    catch (Exception ex)
                    {

                    }
                }
                else
                {
                    new_img = img;
                }
                AdInfo info = new AdInfo()
                {
                    id = id,
                    name = name,
                    img = new_img,
                    link=link,
                    click = click,
                    context = context,
                    createdate = DateTime.Now
                };
                bool ok = false;
                if (id > 0)
                {
                    ok = AdDAL.Update(info);
                }
                else
                {
                    ok = AdDAL.Add(info);
                }
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 保存产品分类
            hs.Add("Save_ProductType", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                string name = JobRequest.GetRequestString("name"); //标题
                
                //参数

                ProductTypeInfo info = new ProductTypeInfo()
                {
                    id = id,
                    name = name,
                    
                };
                bool ok = false;
                if (id > 0)
                {
                    ok = ProductTypeDAL.Update(info);
                }
                else
                {
                    ok = ProductTypeDAL.Add(info);
                }
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 保存画册分类
            hs.Add("Save_ImgType", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                string name = JobRequest.GetRequestString("name"); //标题

                //参数

                ImgTypeInfo info = new ImgTypeInfo()
                {
                    id = id,
                    name = name,

                };
                bool ok = false;
                if (id > 0)
                {
                    ok = ImgTypeDAL.Update(info);
                }
                else
                {
                    ok = ImgTypeDAL.Add(info);
                }
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion
            #region 删除产品
            hs.Add("Delete_Products", () =>
            {
                string ids = JobRequest.GetRequestString("ids");
                bool ok = ProductDAL.Delete(ids);
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 删除产品分类
            hs.Add("Delete_ProductType", () =>
            {
                string ids = JobRequest.GetRequestString("ids");
                bool ok = ProductTypeDAL.Delete(ids);
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 删除画册分类
            hs.Add("Delete_ImgType", () =>
            {
                string ids = JobRequest.GetRequestString("ids");
                bool ok = ImgTypeDAL.Delete(ids);
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 删除画册
            hs.Add("Delete_Imgs", () =>
            {
                string ids = JobRequest.GetRequestString("ids");
                bool ok = ImgDAL.Delete(ids);
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 删除幻灯片
            hs.Add("Delete_Ads", () =>
            {
                string ids = JobRequest.GetRequestString("ids");
                bool ok = AdDAL.Delete(ids);
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 删除咨询
            hs.Add("Delete_News", () =>
            {
                string ids = JobRequest.GetRequestString("ids");
                bool ok = NewsDAL.Delete(ids);
                if (ok)
                {
                    return Commons.GetSuccess("删除成功", "");
                }
                else
                {
                    return Commons.GetFaild("删除失败");
                }
            });
            #endregion
            #region 保存咨询
            hs.Add("Save_Message", () =>
            {
                //参数
                int id = JobRequest.GetRequestInt("id"); // id（非必填）
                string name = JobRequest.GetRequestString("name"); //标题
                string phone = JobRequest.GetRequestString("phone");
                string address = JobRequest.GetRequestString("address");
                string email = JobRequest.GetRequestString("email");
                string context = JobRequest.GetRequestString("context");
                //参数

                MessageInfo info = new MessageInfo()
                {
                    id = id,
                    name = name,
                    phone=phone,
                    address=address,
                    email=email,
                    context=context
                };
                bool ok = false;
                
                ok = MessageDAL.Add(info);
                
                if (ok)
                {
                    return Commons.GetSuccess("保存成功", info);
                }
                else
                {
                    return Commons.GetFaild("保存失败");
                }
            });
            #endregion

            #region 广告点击量+1
            hs.Add("Click_Ad", () =>
            {
                int id = JobRequest.GetRequestInt("id");
                bool ok = AdDAL.Click(id);
                if (ok)
                {
                    return Commons.GetSuccess("成功", "");
                }
                else
                {
                    return Commons.GetFaild("失败");
                }
            });
            #endregion
            #region 资讯点击量+1
            hs.Add("Click_News", () =>
            {
                int id = JobRequest.GetRequestInt("id");
                bool ok = NewsDAL.Click(id);
                if (ok)
                {
                    return Commons.GetSuccess("成功", NewsDAL.FindOneById(id).click);
                }
                else
                {
                    return Commons.GetFaild("失败");
                }
            });
            #endregion

            #region 修改网址配置
            hs.Add("Save_WebSet", () =>
            {
                string title = JobRequest.GetRequestString("title");
                string keyword = JobRequest.GetRequestString("keyword");
                string descript = JobRequest.GetRequestString("descript");
                NewsInfo info = new NewsInfo()
                {
                    id = 1,
                    name = title,
                    img = keyword,
                    click = 0,
                    context = descript,
                    createdate = DateTime.Now
                };
                bool ok = NewsDAL.Update(info);
                if (ok)
                {
                    return Commons.GetSuccess("成功", "");
                }
                else
                {
                    return Commons.GetFaild("失败");
                }
            });
            #endregion
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}